﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Brain
{
    /// <summary>
    /// This is a TimeLine of Animations
    /// </summary>
    public class BaseAnimationTimeLine : IUpdateable, IBrainSonSerialiable
    {
        public virtual List<IBaseAnimation> Animations { get; set; }

        public virtual bool Play { get; set; }

        public bool ShouldUpdate { get; set; }

        public BaseAnimationTimeLine()
        {
            ShouldUpdate = true;
            Play = true;
            ShouldSerialize = true;
        }

        public virtual void AddAnimation(IBaseAnimation Animation)
        {
            if (Animations == null)
            {
                Animations = new List<IBaseAnimation>();
            }
            this.Animations.Add(Animation);
        }

        public bool PlayedAnimation { get; set; }

        public virtual void Update()
        {
            PlayedAnimation = false;

            if (Play && Engine.Instance.AnimationManager.Playing && Animations != null)
            {
                float FrameDif = Engine.Instance.AnimationManager.FrameDifference;
                float CurrentFrame = Engine.Instance.AnimationManager.CurrentFrame;

                for (int i = 0; i < Animations.Count; i++)
                {
                    IBaseAnimation animation = Animations[i];

                    if (animation.Loop && CurrentFrame >= animation.StartFrame)
                    {
                        animation.UpdateLoop(FrameDif);
                        PlayedAnimation = true;
                    }
                    else
                    {
                        if (CurrentFrame >= animation.StartFrame &&
                            CurrentFrame <= animation.AnimationEnd + animation.StartFrame)
                        {
                            PlayedAnimation = true;
                            animation.Update((int)(CurrentFrame - (float)animation.StartFrame));
                        }
                    }
                }
            }
        }

        public bool ShouldSerialize { get; set; }

        public void SerializeData(SerializationData parentData, string add)
        {
            if (this.Animations != null)
            {
                parentData.AddData("TimeLine.Animations", this.Animations.Count, add);
                for (int i = 0; i < this.Animations.Count; i++)
                {
                    IBaseAnimation anim = this.Animations[i];
                    Type t = anim.GetType();
                    parentData.AddData("TimeLine.AnimationType", t.FullName, add + i);
                    parentData.AddData("TimeLine.AnimationAssembly", t.Assembly.GetName().Name, add + i);
                    anim.SerializeData(parentData, add + i);
                }
            }
            else
            {
                parentData.AddData("TimeLine.Animations", 0, add);
            }
        }

        public void DeserializeData(SerializationData parentData, string add)
        {
            int anims = parentData.GetData<int>("TimeLine.Animations", add);
            for (int i = 0; i < anims; i++)
            {
                string type = parentData.GetData<string>("TimeLine.AnimationType", add + i);
                string assembly = parentData.GetData<string>("TimeLine.AnimationAssembly", add + i);
                Assembly ass = Assembly.Load(new AssemblyName(assembly));
                Type t = ass.GetType(type);

                IBaseAnimation anim = (IBaseAnimation)Activator.CreateInstance(t);
            }
        }


        public void DeserializedAll()
        {
        }
    }
}
